import Vue from 'vue'
import VueRouter from 'vue-router'
import manager from "@/views/Manager.vue";
import Home from "@/views/manager/Home.vue";
import user from "@/views/manager/User.vue";
import Auth from "@/views/manager/Auth.vue";
import person from "@/views/manager/Person.vue";
import password from "@/views/manager/Password.vue";
import News from "@/views/manager/News.vue";
import NewsDetail from "@/views/manager/NewsDetail.vue";
import Notice from "@/views/manager/Notice.vue";
import Charts from "@/views/manager/Charts.vue";
import Orders from "@/views/manager/Orders.vue";
// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Manager',
        component: manager,
        redirect: "/home",
        children: [
            {path: "home", name: 'Home', meta: {name: "系统首页"}, component: Home},
            {path: "user", name: 'User', meta: {name: "用户信息"}, component: user},
            {path: "403", name: 'Auth', meta: {name: "无权限"}, component: Auth},
            {path: "person", name: 'Person', meta: {name: "个人信息"}, component: person},
            {path: "password", name: 'Password', meta: {name: "修改密码"}, component: password},
            {path: "news", name: 'News', meta: {name: "新闻信息"}, component: News},
            {path: "newsDetail", name: 'NewsDetail', meta: {name: "新闻信息"}, component: NewsDetail},
            {path: "Notice", name: 'notice', meta: {name: "系统公告"}, component: Notice},
            {path: "Charts", name: 'charts', meta: {name: "数据统计"}, component: Charts},
            {path: "Orders", name: 'orders', meta: {name: "订单管理"}, component: Orders},
        ]
    },
    {
        path: '/login',
        name: 'Login',
        meta: {name: "登录"},
        component: () => import('../views/Login.vue'),
    },
    {
        path: '/register',
        name: 'Register',
        meta: {name: "注册"},
        component: () => import('../views/Register.vue'),
    },
    {
        path: '*',
        name: '404',
        meta: {name: "无法访问"},
        component: () => import('../views/404.vue'),
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    let adminPath = ['/user']
    let user = JSON.parse(localStorage.getItem("honey-user") || `{}`);

    if (user.role !== '管理员' && adminPath.includes(to.path)) {
        next('/403')
    } else {
        next()
    }

})

export default router
